//
//  ViewControllerTwo.m
//  iOSProjectFramework
//
//  Created by 洪冬介 on 2018/1/24.
//  Copyright © 2018年 洪冬介. All rights reserved.
//

#import "ViewControllerTwo.h"
#import "DJSectionModel.h"
#import <Photos/Photos.h>
#import "MeasureView.h"

#define sourcePic(s) [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.bundle/%@", self.fileName,s]]
#define TOP_MARGIN 15

@interface ViewControllerTwo ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MeasureView *measureView;

@end

@implementation ViewControllerTwo{
    double totalHeight;
    NSUInteger _totalPage;
    NSUInteger _currentPage;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = WHITE_COLOR;

    totalHeight = 0.0;
    _currentPage = 0;
    
    
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 765/2.0)];
    headerView.image = [UIImage imageNamed:@"NOTIC"];
    headerView.contentMode = UIViewContentModeScaleAspectFit;
    totalHeight += headerView.size.height;

    
    UIScrollView* scrollView = [UIScrollView new];
    _scrollView = scrollView;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(SCREEN_HEIGHT);
    }];
    [scrollView addSubview:headerView];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [scrollView addGestureRecognizer:tap];
//    scrollView.scrollEnabled = NO;
    scrollView.pagingEnabled = YES;

    NSMutableArray* arr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:self.fileName ofType:@"bundle"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *enumerator;
    enumerator = [fileManager enumeratorAtPath:path];
    while((path = [enumerator nextObject]) != nil) {
        [arr addObject:path];

    }
    
    [arr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [(NSString*)obj1 compare:(NSString*)obj2];
    }];
    
    NSMutableArray* mainPics = [NSMutableArray array];//主图
    NSMutableArray* models = [NSMutableArray array];//模特
    NSMutableArray* products = [NSMutableArray array];//示意
    NSMutableArray* fds = [NSMutableArray array];//细节
    NSMutableArray* dps = [NSMutableArray array];//搭配
    NSMutableArray* tag = [NSMutableArray array];//吊牌

    for (NSString* path in arr) {
        if ([path containsString:@"主图"]) {
            [mainPics addObject:path];
        }else if ([path containsString:@"示意"]){
            [products addObject:path];
        }else if ([path containsString:@"细节"]){
            [fds addObject:path];
        }else if ([path containsString:@"搭配"]){
            [dps addObject:path];
        }else if ([path containsString:@"吊牌"] || [path containsString:@"挂"]){
            [tag addObject:path];
        }else{
            [models addObject:path];
        }
    }
    
    if (mainPics.count) {//主图
        for (NSString* path in mainPics) {
            [self addImageWithPath:path];
        }
    }
        
    if (dps.count) {
        [self addBannerWithImage:[UIImage imageNamed:@"搭配"]];
        for (NSString* path in dps) {
            [self addImageWithPath:path];
        }
    }

    if (products.count) {
        [self addBannerWithImage:[UIImage imageNamed:@"示意"]];
        for (NSString* path in products) {
            [self addImageWithPath:path];
        }
    }

    if (fds.count) {
        [self addBannerWithImage:[UIImage imageNamed:@"细节"]];
        for (NSString* path in fds) {
            [self addImageWithPath:path];
        }
    }
    
    [self addMeasure];
    
    if (models.count) {
        [self addBannerWithImage:[UIImage imageNamed:@"模特"]];
        for (NSString* path in models) {
            [self addImageWithPath:path];
        }
    }

    if (tag.count) {
        [self addBannerWithImage:[UIImage imageNamed:@"吊牌"]];
        for (NSString* path in tag) {
            [self addImageWithPath:path];
        }
    }


    scrollView.contentSize = CGSizeMake(0, totalHeight);
    
    _totalPage = (NSUInteger)ceil(totalHeight / SCREEN_HEIGHT);

}

- (void)addImageWithPath:(NSString*)path{
    NSString *imagePath = sourcePic(path);
    UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
    CGFloat imgH = (SCREEN_WIDTH * image.size.height / image.size.width);
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, imgH)];
    iv.image = image;
    [self.scrollView addSubview:iv];
    totalHeight += (imgH + TOP_MARGIN);
}

- (void)addBannerWithImage:(UIImage*)image{
    CGFloat imgH = (SCREEN_WIDTH * image.size.height / image.size.width);
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, imgH)];
    iv.image = image;
    [self.scrollView addSubview:iv];
    totalHeight += 100;
}

- (void)addMeasure{
    CGFloat imgH = 723 / 2;
    if (!_measureView) {
        _measureView = [[MeasureView alloc] initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, imgH)];
        _measureView.descLab.text = self.descStr;
        _measureView.dataSource = self.measureArr;
        [self.scrollView addSubview:_measureView];
    }
    totalHeight += imgH;
}

- (void)getImage{

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//        [PHAssetChangeRequest creationRequestForAssetFromImage:viewImage];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        DLog(@"%@",success?@"保存成功":@"保存失败");
    }];

}

- (void)tap{
    if (_currentPage + 1 < _totalPage) {
        _currentPage++;
        _scrollView.contentOffset = CGPointMake(0, _currentPage * SCREEN_HEIGHT);
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    DLog(@"停止");
    _currentPage = scrollView.contentOffset.y / SCREEN_HEIGHT;
}

@end
